package com.boyu.common.config;

import com.boyu.common.AttrUtils;
import com.boyu.common.OptionConst;
import com.boyu.common.SystemConst;
import com.boyu.common.utils.StringUtils;
import com.boyu.front.blog.BlogController;
import com.boyu.common.model._MappingKit;
import com.boyu.front.index.IndexController;
import com.boyu.kindeditor.KindeditorController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import javax.swing.border.EmptyBorder;
import java.security.MessageDigest;

/**
 * API引导式配置
 */
public class CmsConfig extends JFinalConfig {

	@Override
	public void afterJFinalStart() {
	}

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", true));
		//me.setViewType(ViewType.FREE_MARKER);
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/kindeditor", KindeditorController.class);
		me.add(new BackendRoutes());
		me.add(new FrontRoutes());
	}

	@Override
	public void configEngine(Engine engine) {
		//公用
		engine.addSharedFunction("/common/_kindeditor.html");
		engine.addSharedFunction("/common/_paginate.html");
		//后台
		engine.addSharedFunction("/bes/common/_layout.html");
		engine.addSharedFunction("/bes/common/_top.html");
		engine.addSharedFunction("/bes/common/_left_menu.html");
		//前台
		engine.addSharedFunction("/fts/_layout.html");
		engine.addSharedFunction("/fts/_foot.html");
		engine.addSharedFunction("/fts/_head.html");
		engine.addSharedFunction("/fts/_top.html");
		engine.addSharedFunction("/fts/_slider.html");
		engine.addSharedFunction("/fts/_left_menu_index.html");
		engine.addSharedFunction("/fts/_left_menu_other.html");
		engine.addSharedFunction("/fts/_news_list.html");

		//工具类
		engine.addSharedObject("sk", new StrKit());
		engine.addSharedObject("su", new StringUtils());
		engine.addSharedObject("OptionConst",new OptionConst());
		engine.addSharedObject("SystemConst",new SystemConst());
		engine.addSharedObject("AttrUtils",new AttrUtils());
	}

	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin C3p0Plugin = createC3p0Plugin();
		me.add(C3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
		me.add(arp);
		
		// 所有配置在 MappingKit 中搞定
		_MappingKit.mapping(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8081, "/", 5);
	}
}
